from rest_framework.response import Response
from rest_framework.views import APIView
from datetime import date, timedelta

from apps.users.models import User


class DailyActiveAPIView(APIView):
    """日活跃用户"""
    def get(self,request):
        # 获取当前日期
        now_day = date.today()
        # 最后登录日期大于等于当前登录日期
        count = User.objects.filter(last_login__gte=now_day).count()

        return Response({'count':count})



class UserActiveAPIView(APIView):
    """日下单用户量统计"""

    def get(self,request):

        today = date.today()
        count = User.objects.filter(orderinfo__create_time__gte=today).count()

        return Response({'count':count})



class UserMonthCountView(APIView):
    """月增用户统计"""
    def get(self,request):
    # 当前日期
        today = date.today()
        # 一个月前日期
        start_day = today-timedelta(days=30)
        data = []

        #循环遍历获取当天日期
        for i in range(30):
            # 当前开始日期
            now_day = start_day+timedelta(days=i)
            # 当前结束日期
            end_day = start_day+timedelta(days=(i+1))
            count = User.objects.filter(date_joined__gte=now_day,
                                        date_joined__lt=end_day).count()

            data.append({
                'count':count,
                'date':now_day
            })

        return Response(data)



class UserCountView(APIView):
    """获取用户总量"""
    def get(self,request):

        today = date.today()
        count = User.objects.all().count()

        return Response({'count':count,'date':today})


class UserincrementAPIView(APIView):
    """获取日增用户"""
    def get(self,request):

        today = date.today()

        count = User.objects.filter(date_joined__gte=today).count()

        return Response({'count':count,'date':today})